/**
 * 
 */
package kr.co.insoft.core.service.driver.api.impl;

import java.util.Collection;
import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.constant.CloudEndPoints;
import kr.co.insoft.core.model.configration.connection.ConnectionEntityModel;
import kr.co.insoft.core.model.configration.vender.VenderEntityModel;
import kr.co.insoft.core.repository.configration.vender.VenderRepository;
import kr.co.insoft.core.service.configration.connection.ConnectionService;
import kr.co.insoft.core.service.driver.api.DriverApiService;
import kr.co.insoft.driver.DriverFactory;
import kr.co.insoft.driver.Drivers;
import kr.co.insoft.driver.exception.DriverException;
import kr.co.insoft.driver.exception.ExceptionCode;
import kr.co.insoft.driver.exception.NotSupportedException;
import kr.co.insoft.driver.model.CloudSponsor;
import kr.co.insoft.driver.model.HardwareProfileModel;
import kr.co.insoft.driver.model.ImageModel;
import kr.co.insoft.driver.model.KeyPairModel;
import kr.co.insoft.driver.model.RegionModel;
import kr.co.insoft.driver.model.SecurityModel;
import kr.co.insoft.driver.model.ZoneModel;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author skpark
 * 
 */
@Component
public class DriverApiServiceImpl implements DriverApiService {

	@Autowired
	VenderRepository venderRepository;

	private static final Logger logger = LoggerFactory
			.getLogger(DriverApiServiceImpl.class);

	@Override
	public Collection<? extends RegionModel> getRegionListByVender(
			String venderid, String accessKey, String secretKey)
			throws ServiceException {

		VenderEntityModel paramVenderModel = new VenderEntityModel();
		VenderEntityModel venderEntityModel = null;
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();

			paramVenderModel.setVenderid(venderid);
			paramVenderModel.setAuth(user);

			venderEntityModel = venderRepository.getVender(paramVenderModel);
		} catch (RepositoryException e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		} catch (Exception e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		}

		Collection<? extends RegionModel> RegionModel = null;
		try {
			Drivers driver = DriverFactory.getDrivers(
					CloudSponsor.valueOf(venderEntityModel.getTypeName()),
					accessKey, secretKey);

			RegionModel = driver.getRegions();
		} catch (NotSupportedException e) {
			return RegionModel;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
		return RegionModel;
	}

	@Override
	public Collection<? extends ZoneModel> getZoneListByRegion(String venderid,
			String regionSeq, String accessKey, String secretKey)
			throws ServiceException {
		VenderEntityModel paramVenderModel = new VenderEntityModel();
		VenderEntityModel venderEntityModel = null;
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();

			paramVenderModel.setVenderid(venderid);
			paramVenderModel.setAuth(user);

			venderEntityModel = venderRepository.getVender(paramVenderModel);
		} catch (RepositoryException e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		} catch (Exception e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		}

		Collection<? extends ZoneModel> zoneModel = null;
		try {
			Drivers driver = DriverFactory.getDrivers(
					CloudSponsor.valueOf(venderEntityModel.getTypeName()),
					accessKey, secretKey);

			driver.setEndpoint(CloudEndPoints.valueOf(
					venderEntityModel.getTypeName())
					.getCloudEndPoint(regionSeq));

			zoneModel = driver.getZones();
		} catch (NotSupportedException e) {
			return zoneModel;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
		return zoneModel;
	}

	@Override
	public Collection<? extends SecurityModel> getSecurityGroupListByRegion(
			String venderid, String regionSeq, String accessKey,
			String secretKey) throws ServiceException {

		VenderEntityModel paramVenderModel = new VenderEntityModel();
		VenderEntityModel venderEntityModel = null;
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();

			paramVenderModel.setVenderid(venderid);
			paramVenderModel.setAuth(user);

			venderEntityModel = venderRepository.getVender(paramVenderModel);
		} catch (RepositoryException e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		} catch (Exception e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		}

		Collection<? extends SecurityModel> SecurityModel = null;
		try {
			Drivers driver = DriverFactory.getDrivers(
					CloudSponsor.valueOf(venderEntityModel.getTypeName()),
					accessKey, secretKey);
			driver.setEndpoint(CloudEndPoints.valueOf(
					venderEntityModel.getTypeName())
					.getCloudEndPoint(regionSeq));

			SecurityModel = driver.getSecures();
		} catch (NotSupportedException e) {
			return SecurityModel;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
		return SecurityModel;
	}

	@Override
	public List<?> getSecurityGroup(String venderid, String regionSeq,
			String securityGroupid, String accessKey, String secretKey)
			throws ServiceException {
		VenderEntityModel paramVenderModel = new VenderEntityModel();
		VenderEntityModel venderEntityModel = null;
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();

			paramVenderModel.setVenderid(venderid);
			paramVenderModel.setAuth(user);

			venderEntityModel = venderRepository.getVender(paramVenderModel);
		} catch (RepositoryException e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		} catch (Exception e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		}

		List<?> ipPermissions = null;
		try {
			Drivers driver = DriverFactory.getDrivers(
					CloudSponsor.valueOf(venderEntityModel.getTypeName()),
					accessKey, secretKey);
			driver.setEndpoint(CloudEndPoints.valueOf(
					venderEntityModel.getTypeName())
					.getCloudEndPoint(regionSeq));

			for (SecurityModel security : driver.getSecures()) {
				if (security.getGroupId().equals(securityGroupid))
					ipPermissions = security.getIpPermissions();
			}
		} catch (NotSupportedException e) {
			return ipPermissions;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
		return ipPermissions;
	}

	@Override
	public Collection<? extends KeyPairModel> getKeyPairListByRegion(
			String venderid, String regionSeq, String accessKey,
			String secretKey) throws ServiceException {
		VenderEntityModel paramVenderModel = new VenderEntityModel();
		VenderEntityModel venderEntityModel = null;
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();

			paramVenderModel.setVenderid(venderid);
			paramVenderModel.setAuth(user);

			venderEntityModel = venderRepository.getVender(paramVenderModel);
		} catch (RepositoryException e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		} catch (Exception e) {
			throw new ServiceException(
					kr.co.insoft.framework.exceptions.ExceptionCode.SERVICE_EXCEPTION,
					e);
		}

		Collection<? extends KeyPairModel> KeyPairModel = null;
		try {
			Drivers driver = DriverFactory.getDrivers(
					CloudSponsor.valueOf(venderEntityModel.getTypeName()),
					accessKey, secretKey);
			driver.setEndpoint(CloudEndPoints.valueOf(
					venderEntityModel.getTypeName())
					.getCloudEndPoint(regionSeq));

			KeyPairModel = driver.getKeyPairs();
		} catch (NotSupportedException e) {
			return KeyPairModel;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
		return KeyPairModel;
	}

	@Autowired
	ConnectionService connectionService;

	@Override
	public Collection<? extends ImageModel> getMyImages(String connectionid)
			throws ServiceException {
		Drivers drivers;
		try {
			ConnectionEntityModel connectionEntityModel = connectionService
					.getArticle(connectionid);

			drivers = DriverFactory.getDrivers(
					CloudSponsor.valueOf(connectionEntityModel.getTypeName()),
					connectionEntityModel.getAccessKey(),
					connectionEntityModel.getSecretKey());
			drivers.setEndpoint(CloudEndPoints.valueOf(
					connectionEntityModel.getTypeName()).getCloudEndPoint(
					connectionEntityModel.getRegionSeq()));
			return drivers.getMyImages();
		} catch (NotSupportedException e) {
			return null;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
	}
	
	@Override
	public Collection<? extends HardwareProfileModel> getHardwareProfiles(
			String connectionid) throws ServiceException {
		Drivers drivers;
		try {
			ConnectionEntityModel connectionEntityModel = connectionService
					.getArticle(connectionid);

			drivers = DriverFactory.getDrivers(
					CloudSponsor.valueOf(connectionEntityModel.getTypeName()),
					connectionEntityModel.getAccessKey(),
					connectionEntityModel.getSecretKey());
			drivers.setEndpoint(CloudEndPoints.valueOf(
					connectionEntityModel.getTypeName()).getCloudEndPoint(
					connectionEntityModel.getRegionSeq()));
			return drivers.getHardwareProfiles();
		} catch (NotSupportedException e) {
			return null;
		} catch (DriverException e) {
			throw new ServiceException(ExceptionCode.DRIVER_ERROR, e);
		}
	}
}
